package com.wms.presenters;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.gatech.wms.R;
import com.wms.assets.Categories;
import com.wms.assets.Item;
import com.wms.assets.Status;
import com.wms.model.ItemsModel;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

/**
 * Presenter for the screen that lists all of the lost items for a user
 * @TODO turn this into a fragment and put it in the User's profile
 * @author Nathan
 */
public class LostItemListActivity extends WMSActivity {
	
	private static final String TAG = "WMS";
	private ItemsModel model = null;
	private Map<String, Boolean> cat;
	private int maxDay = 0;
	private int maxMonth = 0;
	private int maxYear = 99999;
	private boolean found = false;

	/**
	 * Initializes this activity by setting the layout.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lost_items_screen);
		model = ItemsModel.getInstance();
		cat = new HashMap<String, Boolean>();
		cat.put(Categories.ACCESSORIES, false);
		cat.put(Categories.ANIMALS, false);
		cat.put(Categories.CLOTHING, false);
		cat.put(Categories.ELECTRONICS, false);
		cat.put(Categories.TOYS, false);
		cat.put(Categories.MISCELLANY, false);
		setupButtons();
		populateItems();
	}
	
	/**
	 * Sets the clickListeners for all the buttons
	 */
	private void setupButtons() {
		Button additem = (Button)findViewById(R.id.button1);
		
		additem.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(LostItemListActivity.this, ItemEntryActivity.class);
				i.putExtra("mode", Status.LOST);
				startActivityForResult(i, 12);
			}
		});
		
		Button filter = (Button)findViewById(R.id.button2);
		filter.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(LostItemListActivity.this, FilterScreenActivity.class);
        		startActivityForResult(i, 0);
			}
		});
	}
	
	/**
	 * called when any activity finishes when called by startActivityForResult 
	 * handles refreshing of items list
	 */
	protected void onActivityResult(int code, int result, Intent data) {
		super.onActivityResult(code, result, data);
		if (result == 10) {
			populateItems();	
		}
		else if(result == 15){
			Bundle extras = data.getExtras();
			cat.put(Categories.ACCESSORIES, extras.getBoolean(Categories.ACCESSORIES));
			cat.put(Categories.ANIMALS, extras.getBoolean(Categories.ANIMALS));
			cat.put(Categories.CLOTHING, extras.getBoolean(Categories.CLOTHING));
			cat.put(Categories.ELECTRONICS, extras.getBoolean(Categories.ELECTRONICS));
			cat.put(Categories.TOYS, extras.getBoolean(Categories.TOYS));
			cat.put(Categories.MISCELLANY, extras.getBoolean(Categories.MISCELLANY));
			maxYear = extras.getInt("year");
			if (maxYear == -1)
				maxYear = 99999;
			maxMonth = extras.getInt("month");
			maxDay = extras.getInt("day");
			found = extras.getBoolean("found");
			populateItems();
		}
	}
	
	/**
	 * fills the list with items from the database
	 * @param items - a list of NEW items to be added to the list and database
	 */
	private void populateItems() {
		ListView view = (ListView)this.findViewById(R.id.listView1);
		ArrayList<String> names = new ArrayList<String>();
		
		try {
			for (Item it : model.getItems()) {
				boolean test = ((cat.get(Categories.ACCESSORIES) && it.isCategory(Categories.ACCESSORIES))
						|| (cat.get(Categories.ANIMALS) && it.isCategory(Categories.ANIMALS))
						|| (cat.get(Categories.CLOTHING) && it.isCategory(Categories.CLOTHING))
						|| (cat.get(Categories.ELECTRONICS) && it.isCategory(Categories.ELECTRONICS))
						|| (cat.get(Categories.TOYS) && it.isCategory(Categories.TOYS))
						|| (cat.get(Categories.MISCELLANY) && it.isCategory(Categories.MISCELLANY))
						|| catClear());
				if (!found && it.status.equals(Status.LOST) && test) {
					
					if (it.year < maxYear) {
						names.add(it.name);	
					} else if (it.year == maxYear) {
						if (it.month < maxMonth) {
							names.add(it.name);
						}
						else if (it.month == maxMonth) {
							if (it.day <= maxDay) {
								names.add(it.name);
							}
						}
					}
				}
			}
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage(), e);
		} catch (InstantiationException e) {
			Log.e(TAG, e.getMessage(), e);
		} catch (IllegalAccessException e) {
			Log.e(TAG, e.getMessage(), e);
		} catch (ClassNotFoundException e) {
			Log.e(TAG, e.getMessage(), e);
		}
		
		String[] strings = new String[names.size()];
		names.toArray(strings);
		view.setAdapter(new ArrayAdapter<String>(this, R.layout.item_renderer, strings));
	}
	
	private Boolean catClear(){
		if(!cat.get(Categories.ACCESSORIES)&&!cat.get(Categories.ANIMALS)&&
				!cat.get(Categories.CLOTHING)&&!cat.get(Categories.ELECTRONICS)&&
				!cat.get(Categories.MISCELLANY)&&!cat.get(Categories.TOYS))
			return true;
		else return false;
	}
}
